/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
    let res = 0
    let minPrice = prices[0]
    for (let i = 1; i < prices.length; i++) {
        minPrice = prices[i] < minPrice ? prices[i] : minPrice //维护目前已知的最小价格
        res = prices[i] - minPrice > res ? prices[i] - minPrice : res //用当前价格减去已知的最小价格，维护最大差值
    }
    return res
};

let prices = [7, 1, 5, 3, 6, 4]
console.log(maxProfit(prices));